package user

import (
	"github.com/go-playground/validator/v10"
	user2 "server/common/models/user"
	"time"
)

type MomentCreate struct {
	Content    string `json:"content"`                        // 文字内容最多300
	Images     string `json:"images"`                         // 图片
	Status     string `json:"status" binding:"required"`      // 仅自己可见 just_me 仅主页可见 just_index 所有人可见 all
	UserStatus string `json:"user_status" binding:"required"` // hide 隐身 show 显示
	Type       string `json:"type" binding:"required"`        // text文字 image_text 图文 vote 投票
	Subject    string `json:"subject"`                        // 主题话题
}

func (m *MomentCreate) GetError(err any) string {
	for _, e := range err.(validator.ValidationErrors) {
		if e.Field() == "Content" {
			return "内容不能为空"
		}
		if e.Field() == "Status" {
			return "可见设置不能为空"
		}
		if e.Field() == "UserStatus" {
			return "用户显示状态不能为空"
		}
		if e.Field() == "Type" {
			return "发表类型不能为空"
		}
	}
	return "参数不完整"
}

func (m *MomentCreate) ToModel(userId int64) *user2.UserMoment {
	now := time.Now().Unix()
	return &user2.UserMoment{
		UserId:     userId,
		Content:    m.Content,
		Images:     m.Images,
		Status:     m.Status,
		UserStatus: m.UserStatus,
		Type:       m.Type,
		Subject:    m.Subject,
		CreateTime: now,
		UpdateTime: now,
	}
}
